<template>
    <h2>会员等级添加页面</h2>
    <table border="1">
        <tr>
            <td>等级编号</td>
            <td>
                <input type="text" v-model="rank.MR_Code"/>
            </td>
            <td>等级名称</td>
            <td>
                <input type="text" v-model="rank.MR_Name"/>
            </td>          
        </tr>
        <tr>
            <td>优惠方案</td>
            <td colspan="3">
                <select v-model="rank.MR_Ideas">
        <option value="泰康保险">泰康保险</option>
        <option value="平安保险">平安保险</option>
        <option value="360报销">360报销</option>
    </select>
            </td>
        </tr>
        <tr>
            <td>消费下限</td>
            <td>
                <input type="text" v-model="rank.MR_MinPrice"/>
            </td>
            <td>消费上限</td>
            <td>
                <input type="text" v-model="rank.MR_MaxPrice"/>
            </td>
        </tr>
        <tr>
            <td colspan="4">
                每消费 <input type="radio" value="折前" v-model="rank.MR_UseMethod"/>折前
                <input type="radio" value="折后" v-model="rank.MR_UseMethod"/>折后
                <input type="text" style="width:30px" v-model="rank.MR_EachScore"/>元积1积分
            </td>
        </tr>            
        <tr>
            <td>打折比例</td>
            <td>
                <input type="text" v-model="rank.MR_Discount"/>
            </td>
            <td>初始积分</td>
            <td>
                <input type="text" v-model="rank.MR_BeginScore"/>
            </td>
        </tr>
        <tr>
            <td>积分区间</td>
            <td colspan="3">
                <input type="text" v-model="rank.MR_MinScore"/>~
                <input type="text" v-model="rank.MR_MaxScore"/>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="添加" @click="addRank" />
            </td>
        </tr>
    </table>
</template>

<script setup lang="ts">
    import {ref} from 'vue'
    import {useRouter} from 'vue-router'
    const router=useRouter();
    import axios from 'axios'

    const rank=ref({
      "MR_Id": 1,
      "MR_Code": "",
      "MR_Name": "",
      "MR_Ideas": "泰康保险",
      "MR_MinPrice": 0,
      "MR_MaxPrice": 0,
      "MR_UseMethod": "折前",
      "MR_EachScore": 1,
      "MR_Discount": null,
      "MR_BeginScore": 0,
      "MR_MinScore": 0,
      "MR_MaxScore": 0,
      "MR_CreateTime": "2024-09-05T00:00:00"
    })
    const addRank=()=>{
        console.log(rank.value.MR_Name);
        if(rank.value.MR_Code=="")
        {
            alert('不能为空');
            return;
        }
        if(rank.value.MR_Name=="")
        {
            alert('等级名称不能为空');
            return;
        }    
        if(rank.value.MR_Ideas=="")
        {
            alert('不能为空');
            return;
        }
        if(rank.value.MR_MinPrice=="")
        {
            alert('不能为空');
            return;
        }
        if(rank.value.MR_MaxPrice=="")
        {
            alert('不能为空');
            return;
        }
        if(rank.value.MR_UseMethod=="")
        {
            alert('不能为空');
            return;
        }
        if(rank.value.MR_EachScore=="")
        {
            alert('等级名称不能为空');
            return;
        }
        if(rank.value.MR_BeginScore=="")
        {
            alert('不能为空');
            return;
        }
        if(rank.value.MR_MinScore=="")
        {
            alert('等级名称不能为空');
            return;
        }
        if(rank.value.MR_MaxScore=="")
        {
            alert('不能为空');
            return;
        }

        axios.post('https://localhost:7292/api/Member/RankAdd',{
            "MR_Code": rank.value.MR_Code,
      "MR_Name":  rank.value.MR_Name,
      "MR_Ideas":  rank.value.MR_Ideas,
      "MR_MinPrice": rank.value.MR_MinPrice,
      "MR_MaxPrice": rank.value.MR_MaxPrice,
      "MR_UseMethod":  rank.value.MR_UseMethod,
      "MR_EachScore":  rank.value.MR_EachScore,
      "MR_Discount":  rank.value.MR_Discount,
      "MR_BeginScore":  rank.value.MR_BeginScore,
      "MR_MinScore": rank.value.MR_MinScore,
      "MR_MaxScore": rank.value.MR_MaxScore,
      "MR_CreateTime": "2024-09-05T00:00:00"
        })
        .then(res=>{
            if(res.data==-1)
            {
                alert('等级名称不能重复');
            }
            else if(res.data>0)
            {
                alert('添加成功');
                router.push('/showRank');
            }
            else
            {
                alert('添加失败');
            }
        })
    }

</script>